//
//  LSZAnswerParseViewController.m
//  PartyClassCloud
//
//  Created by 梁素珍 on 2018/9/7.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZAnswerParseViewController.h"

@interface LSZAnswerParseViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UIView * viewWrong;
@property(nonatomic,strong) UITableView * tableViewWrong;

@property(nonatomic,strong) NSArray * arrayM;
@end

@implementation LSZAnswerParseViewController
#pragma mark - 提交分数
-(void)initWIthTiJiaofenshu
{
    [LSZRequestData initWithPassTheData:self.id_Id :@"60" :^(BOOL isOk, NSString * string, NSDictionary * dic) {
        if(isOk == YES){
            //分数提交成功;
            [self initWithtring:@"分数提交成功!"];
        }
    }];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[LSZAnswerQuestionsViewController class]]) {
            LSZAnswerQuestionsViewController *messageVC =(LSZAnswerQuestionsViewController *)controller;
            [self.navigationController popToViewController:messageVC animated:YES];
        } 
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"考试结果";
    [self setScoreUI];
    self.arrayM =[NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z", nil];

}
-(void)setScoreUI
{
    UIView * viewTop = [[UIView alloc]init];
    viewTop.frame = CGRectMake(0,NAVIGATION_BAR_HEIGHT, ScreenWidth, 100);
    viewTop.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:viewTop];
//    NSLog(@"%ld>>>>>>>%ld",self.rightNumber/self.totalNumber,self.rightNumber%self.totalNumber);
//    NSLog(@"%ld>>>>>>>%ld",self.totalNumber/self.rightNumber,self.totalNumber%self.rightNumber);

    CGFloat  score =(CGFloat)self.rightNumber/self.totalNumber  *100;
    if(score >= 60){
        [self initWIthTiJiaofenshu];
    }
    UILabel * labelScore = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, ScreenWidth-40, 40)];
    labelScore.textColor = lightColor;
    labelScore.font = FONT_(20);
    labelScore.text = [NSString stringWithFormat:@"得分:%.2f",score];
    [viewTop addSubview:labelScore];
    
    
    UILabel * labelTotal = [[UILabel alloc]init];
    labelTotal.frame = CGRectMake(0, 10+CGRectGetMaxY(labelScore.frame), ScreenWidth/3, viewTop.frame.size.height-20-CGRectGetMaxY(labelScore.frame));
    labelTotal.textColor = titleColorLSZ;
    labelTotal.font = FONT_(15);
    labelTotal.textAlignment = NSTextAlignmentCenter;
    labelTotal.text = [NSString stringWithFormat:@"总共%ld题",(long)self.totalNumber];
    [viewTop addSubview:labelTotal];
    
    
    
    UILabel * labelAnswerCorrectly = [[UILabel alloc]init];
    labelAnswerCorrectly.frame = CGRectMake(CGRectGetMaxX(labelTotal.frame), 10+CGRectGetMaxY(labelScore.frame), ScreenWidth/3, viewTop.frame.size.height-20-CGRectGetMaxY(labelScore.frame));
    labelAnswerCorrectly.textColor = titleColorLSZ;
    labelAnswerCorrectly.font = FONT_(15);
    labelAnswerCorrectly.textAlignment = NSTextAlignmentCenter;
    labelAnswerCorrectly.text = [NSString stringWithFormat:@"答对%ld题",(long)self.rightNumber];
    [viewTop addSubview:labelAnswerCorrectly];
    
    
    UILabel * labelWrong = [[UILabel alloc]init];
    labelWrong.frame = CGRectMake(CGRectGetMaxX(labelAnswerCorrectly.frame), 10+CGRectGetMaxY(labelScore.frame), ScreenWidth/3, viewTop.frame.size.height-20-CGRectGetMaxY(labelScore.frame));
    labelWrong.textColor = titleColorLSZ;
    labelWrong.font = FONT_(15);
    labelWrong.textAlignment = NSTextAlignmentCenter;
    labelWrong.text = [NSString stringWithFormat:@"答错%ld题",(long)self.wrongNumber];
    [viewTop addSubview:labelWrong];
    
    UIView * viewLineOne = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(labelScore.frame)+5, ScreenWidth, 0.5)];
    viewLineOne.backgroundColor = dividerColor;
    [viewTop addSubview:viewLineOne];
    
    UIView * viewLineTwo = [[UIView alloc]initWithFrame:CGRectMake(ScreenWidth/3, CGRectGetMaxY(labelScore.frame)+7, 1, viewTop.frame.size.height-CGRectGetMaxY(labelScore.frame)-10)];
    viewLineTwo.backgroundColor = dividerColor;
    [viewTop addSubview:viewLineTwo];
    
    UIView * viewLineThree = [[UIView alloc]initWithFrame:CGRectMake(ScreenWidth/3*2, CGRectGetMaxY(labelScore.frame)+7, 1, viewTop.frame.size.height-CGRectGetMaxY(labelScore.frame)-10)];
    viewLineThree.backgroundColor = dividerColor;
    [viewTop addSubview:viewLineThree];
    
    self.viewWrong = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(viewTop.frame)+10, ScreenWidth, 50)];
    self.viewWrong.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewWrong];
    
    UIView * viewRed = [[UIView alloc]init];
    viewRed.frame = CGRectMake(10, 10, 2, self.viewWrong.frame.size.height-20);
    viewRed.backgroundColor = themeColor;
    [self.viewWrong addSubview:viewRed];
    
    UILabel * labelWring = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, ScreenWidth-40, self.viewWrong.frame.size.height-20)];
    labelWring.text = @"答错的题目";
    labelWring.textColor = titleColorLSZ;
    labelWring.font = FONT_(15);
    [self.viewWrong addSubview:labelWring];
    [self.tableViewWrong registerClass:[LSZParsingTitleTableViewCell class] forCellReuseIdentifier:@"LSZParsingTitleTableViewCell"];
    [self.tableViewWrong registerClass:[LSZAnswerParseTableViewCell class] forCellReuseIdentifier:@"LSZAnswerParseTableViewCell"];

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arrayMg.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    LSZAnswerQuestionsDetailsMg * mg = nil;
    mg = self.arrayMg[section];
    return 2+ mg.arrayContent.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    LSZAnswerQuestionsDetailsMg * mg = nil;
    mg = self.arrayMg[section];
    if (section == 1+ mg.arrayContent.count) {
        return 20;
    }
    return 0.0001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.arrayMg.count >indexPath.section) {
        LSZAnswerQuestionsDetailsMg * mg = nil;
        mg = self.arrayMg[indexPath.section];
        if (indexPath.row == 0) {
            NSString * str = [[NSString alloc]init];
            if ([mg.examType isEqualToString:@"1"]) {
                str = [NSString stringWithFormat:@"%@%@",@"[单选题]",mg.name];
            }else if ([mg.examType isEqualToString:@"0"]){
                str = [NSString stringWithFormat:@"%@%@",@"[判断题]",mg.name];
            }else{
                str = [NSString stringWithFormat:@"%@%@",@"[多选题]",mg.name];
            }
            CGFloat height = [LSZSpecialPackaging getHeightOfText:ScreenWidth-60 height:ScreenHeight font:14 :str];
            return height;//可变 标题
        }else if (indexPath.row == mg.arrayContent.count+1){
            CGFloat height = [LSZSpecialPackaging getHeightOfText:ScreenWidth-60 height:ScreenHeight font:14 :mg.answer];
            return height;//答案
        }else{
            NSString * strTitle = [NSString stringWithFormat:@"%@:%@",self.arrayM[indexPath.row-1],mg.arrayContent[indexPath.row-1]];
            CGFloat height = [LSZSpecialPackaging getHeightOfText:ScreenWidth-60 height:ScreenHeight font:14 :strTitle];
            return height;//选项
        }
    }else{
        return 0.001;
    }
   
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZAnswerQuestionsDetailsMg * mg = nil;
    mg = self.arrayMg[indexPath.section];
    if ( indexPath.row == mg.arrayContent.count+1){
        //错误的答案
        LSZAnswerParseTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZAnswerParseTableViewCell" forIndexPath:indexPath];
        if ([mg.examType isEqualToString:@"1"]) {
            NSInteger A = [mg.answer integerValue];
            cell.labelCorrect.text = [NSString stringWithFormat:@"正确: %@",self.arrayM[A]];
            cell.labelWrong.text = [NSString stringWithFormat:@"错误: %@",self.arrayM[mg.number]];
        }else if ([mg.examType isEqualToString:@"0"]){
            NSInteger A = [mg.answer integerValue];
            cell.labelCorrect.text = [NSString stringWithFormat:@"正确: %@",self.arrayM[A]];
            //判断题
            if(self.arrayM[A] == 0){
                cell.labelCorrect.text = [NSString stringWithFormat:@"正确:是"];
                cell.labelWrong.text = [NSString stringWithFormat:@"错误: 否"];
            }else{
                cell.labelCorrect.text = [NSString stringWithFormat:@"正确:否"];
                cell.labelWrong.text = [NSString stringWithFormat:@"错误: 是"];
            }
        }else{
            NSArray  *arrayAnswer = [mg.answer componentsSeparatedByString:@","];
 
            NSString * stringAnswer = [[NSString alloc]init];
            for (int i = 0; i<arrayAnswer.count; i++) {
               NSInteger A = [arrayAnswer[i] integerValue];
                if (i == 0) {
                    stringAnswer = _arrayM[A];
                }else{
                    stringAnswer = [NSString stringWithFormat:@"%@,%@",stringAnswer,_arrayM[A]];
                }
            }
            cell.labelCorrect.text = [NSString stringWithFormat:@"正确:%@",stringAnswer];
            
  
            NSString * stringWrong = [[NSString alloc]init];
            NSLog(@"%@>>>>", mg.addAnswerArray);

            for (int i = 0; i< mg.addAnswerArray.count; i++) {
                NSInteger A = [mg.addAnswerArray[i] integerValue];
                if (i == 0) {
                    stringWrong = _arrayM[A];
                }else{
                    stringWrong = [NSString stringWithFormat:@"%@,%@",stringWrong,_arrayM[A]];
                }
            }
            NSLog(@"%@>>>>",stringWrong);
            cell.labelWrong.text = [NSString stringWithFormat:@"错误:%@",stringWrong];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        LSZParsingTitleTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZParsingTitleTableViewCell" forIndexPath:indexPath];
        if(indexPath.row == 0){
            cell.labelTitle.textColor = titleColorLSZ;
            cell.labelNumber.text = [NSString stringWithFormat:@"%ld",(long)indexPath.section+1];
            cell.labelNumber.hidden = NO;
            NSString * str = [[NSString alloc]init];
            if ([mg.examType isEqualToString:@"1"]) {
                str = [NSString stringWithFormat:@"%@%@",@"[单选题]",mg.name];
            }else if ([mg.examType isEqualToString:@"0"]){
                str = [NSString stringWithFormat:@"%@%@",@"[判断题]",mg.name];
            }else{
                str = [NSString stringWithFormat:@"%@%@",@"[多选题]",mg.name];
            }
            cell.labelTitle.text = str;
            CGFloat height = [LSZSpecialPackaging getHeightOfText:ScreenWidth-60 height:ScreenHeight font:14 :str];
            CGRect frame = cell.labelTitle.frame;
            frame.size.height = height;
            cell.labelTitle.frame = frame;

        }else{
            cell.labelTitle.textColor = lightColor;
            cell.labelNumber.hidden = YES;
            if (self.arrayM.count >indexPath.row-1 && mg.arrayContent.count >indexPath.row-1) {
                NSString * strTitle = [NSString stringWithFormat:@"%@:%@",self.arrayM[indexPath.row-1],mg.arrayContent[indexPath.row-1]];
                cell.labelTitle.text = strTitle;
                CGFloat height = [LSZSpecialPackaging getHeightOfText:ScreenWidth-60 height:ScreenHeight font:14 :strTitle];
                CGRect frame = cell.labelTitle.frame;
                frame.size.height = height;
                cell.labelTitle.frame = frame;
            }
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
-(UITableView *)tableViewWrong
{
    if (_tableViewWrong == nil) {
        _tableViewWrong = [[UITableView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.viewWrong.frame),ScreenWidth, ScreenHeight-CGRectGetMaxY(self.viewWrong.frame)-HOME_INDICATOR_HEIGHT) style:UITableViewStylePlain];
        _tableViewWrong.delegate = self;
        _tableViewWrong.dataSource = self;
        _tableViewWrong.estimatedRowHeight=0;
        _tableViewWrong.estimatedSectionFooterHeight =0;
        _tableViewWrong.estimatedSectionHeaderHeight =0;
        _tableViewWrong.separatorStyle = UITableViewCellEditingStyleNone;     //让tableview不显示分割线
        [self.view addSubview:_tableViewWrong];
    }
    return _tableViewWrong;
}
@end
